package com.axend.lib_base.utils

import android.os.Build
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.Period
import java.time.format.DateTimeFormatter
import java.util.Calendar
import java.util.Date
import java.util.Locale


object DateUtils {

    /**
     * Int根据日期计算年龄
     */
    @Throws(Exception::class)
    fun calculateAge(birthDateString: String): Int {
        if (birthDateString.isEmpty()) return 0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 定义与日期字符串匹配的格式器
            val formatter = DateTimeFormatter.ofPattern("yyyy-M-d")
            val birthDate = LocalDate.parse(birthDateString,formatter)
            val currentDate = LocalDate.now()

            // 确保出生日期不晚于当前日期
            if (birthDate.isAfter(currentDate)) {
                throw IllegalArgumentException("出生日期不能晚于当前日期")
            }

            return Period.between(birthDate, currentDate).years
        } else {
            val dateFormat = SimpleDateFormat("yyyy-M-d")
            val birthDate = dateFormat.parse(birthDateString) ?: throw Exception("无法解析出生日期")
            val calendarBirth = Calendar.getInstance()
            calendarBirth.time = birthDate

            val calendarToday = Calendar.getInstance()

            var age = calendarToday.get(Calendar.YEAR) - calendarBirth.get(Calendar.YEAR)
            if (calendarToday.get(Calendar.MONTH) < calendarBirth.get(Calendar.MONTH)
                || (calendarToday.get(Calendar.MONTH) == calendarBirth.get(Calendar.MONTH)
                        && calendarToday.get(Calendar.DAY_OF_MONTH) < calendarBirth.get(Calendar.DAY_OF_MONTH))
            ) {
                age--
            }

            return age
        }

    }

    /**
     * 计算时间戳(默认两分钟)
     */
    fun isTimeStampGreaterThan(timestamp: Long,millis:Long=20 * 1000L): Boolean {
        val currentTime = System.currentTimeMillis()
        val timeDifference = currentTime - timestamp
        return timeDifference > millis
    }


    /**
     * 获取当前时间
     */
    fun getCurrentTime(): String {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        return dateFormat.format(Calendar.getInstance().time)
    }

    /**
     * 获取当前时间(自定义格式)
     */
    fun getCurrentTime(dateFormat:String): String {
        val dateFormat = SimpleDateFormat(dateFormat)
        return dateFormat.format(Calendar.getInstance().time)
    }

    /**
     * 获取当前时间，精确到毫秒
     */
    fun getCurrentFileTimeStamp(): String {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSS")
        return dateFormat.format(Calendar.getInstance().time)
    }

    /**
     * 获取当前时间不要日期
     */
    fun getCurrentTimeNoDate(): String {
        val dateFormat = SimpleDateFormat("HH:mm")
        return dateFormat.format(Calendar.getInstance().time)
    }

    /**
     * 获取当前日期
     */
    fun getCurrentDate(): String {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd")
        return dateFormat.format(Calendar.getInstance().time)
    }

    /**
     * 获取当前时间戳
     */
    fun getCurrentTimeStamp(): Long {
        return System.currentTimeMillis()
    }

    /**
     * 获取当前时间并格式化为指定格式的字符串
     * @return 当前时间的格式化字符串
     */
    fun getCurrentDateTime(): String? {
        // 创建一个SimpleDateFormat对象，指定日期时间格式
        val sdf = SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.US)

        // 获取当前时间
        val now = Date()

        // 使用SimpleDateFormat格式化当前时间
        return sdf.format(now)
    }
}